package com.ht.pms.core.dao.impl;

import com.ht.pms.core.dao.NoticeRecordDao;
import com.ht.pms.core.dto.NoticeRecord;
import com.ht.pms.core.dto.QueryNoticeRecordReq;
import com.ht.pms.core.dto.send.EmailResult;
import com.ht.pms.core.dto.send.NoticeEmailRecordDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author:喻尊龙
 * @date: 2018/4/23
 */
@Service("noticeRecordDao")
public class NoticeRecordDaoImpl implements NoticeRecordDao {

    @Autowired
    private JdbcTemplate template;

    @Override
    public List<EmailResult> queryNoticeRecords(QueryNoticeRecordReq req,String tableName) {
        StringBuilder sql = new StringBuilder();
        sql = sql.append("SELECT t1.* " +
                "FROM ");
        sql.append(tableName+" t1 WHERE 1=1 ");
        if (StringUtils.isNotBlank(req.getDataTime())) {
            sql.append(" AND t1.send_ym like '"+req.getDataTime()+"%' ");
        }
        if (StringUtils.isNotBlank(req.getSendState())) {
            sql.append(" AND t1.send_state='"+req.getSendState()+"' ");
        }
        if (StringUtils.isNotBlank(req.getConfirmState())) {
            sql.append(" AND t1.confirm_state='"+req.getConfirmState()+"' ");
        }
        if (StringUtils.isNotBlank(req.getSendType())) {
            sql.append(" AND t1.send_type='"+req.getSendType()+"' ");
        }
        if (StringUtils.isNotBlank(req.getKey())) {
            sql.append(" AND (t1.job_code like '%"+req.getKey()+"%' or t1.name like '%"+req.getKey()+"%') ");
        }
        int offSet = (req.getPage()-1)*req.getLimit();
        sql.append(" ORDER BY t1.create_time DESC LIMIT "+offSet+","+req.getLimit()+"");

        return template.query(sql.toString(),new BeanPropertyRowMapper(EmailResult.class));
    }

    @Override
    public int queryNoticeRecordsCount(QueryNoticeRecordReq req,String tableName) {
        StringBuilder sql = new StringBuilder();
        sql = sql.append("SELECT count(*) as count FROM  ");
        sql.append(tableName+" t1 WHERE 1=1 ");
        if (StringUtils.isNotBlank(req.getDataTime())) {
            sql.append(" AND t1.send_ym like '"+req.getDataTime()+"%' ");
        }
        if (StringUtils.isNotBlank(req.getSendState())) {
            sql.append(" AND t1.send_state='"+req.getSendState()+"' ");
        }
        if (StringUtils.isNotBlank(req.getConfirmState())) {
            sql.append(" AND t1.confirm_state='"+req.getConfirmState()+"' ");
        }
        if (StringUtils.isNotBlank(req.getSendType())) {
            sql.append(" AND t1.send_type='"+req.getSendType()+"' ");
        }
        if (StringUtils.isNotBlank(req.getKey())) {
            sql.append(" AND (t1.job_code like '%"+req.getKey()+"%' or t1.name like '%"+req.getKey()+"%') ");
        }
        Map<String,Object> result = template.queryForMap(sql.toString());
        if(result != null){
            return Integer.parseInt(result.get("count")+"");
        }
        return 0;
    }
}
